package com.elegant.code.design.machine;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.statemachine.config.EnableStateMachine;
import org.springframework.statemachine.config.StateMachineConfigurerAdapter;
import org.springframework.statemachine.config.builders.StateMachineConfigurationConfigurer;
import org.springframework.statemachine.config.builders.StateMachineStateConfigurer;
import org.springframework.statemachine.config.builders.StateMachineTransitionConfigurer;
import org.springframework.statemachine.ensemble.StateMachineEnsemble;
import org.springframework.statemachine.zookeeper.ZookeeperStateMachineEnsemble;

import java.util.EnumSet;
import static com.elegant.code.design.machine.OrderStatus.*;
import static com.elegant.code.design.machine.OrderEvent.*;

@Configuration
@EnableStateMachine
public class OrderStateMachineConfig extends StateMachineConfigurerAdapter<OrderStatus, OrderEvent> {
    @Autowired//这个监听器做具体的数据库操作
    private StateMachineDaoListener stateMachineDaoListener;
    @Bean//使用zookeeper，让stateMachine支持分布式
    public StateMachineEnsemble<OrderStatus, OrderEvent> stateMachineEnsemble() throws Exception {
        return new ZookeeperStateMachineEnsemble<OrderStatus, OrderEvent>(curatorClient(), "/orderStatus");
    }
    @Override
    public void configure(StateMachineTransitionConfigurer<OrderStatus, OrderEvent> transitions)
            throws Exception {
        transitions
                .withExternal()
                .source(GENERATED).target(AUDIT_WATING_BY_CDM)
                .event(GENERATE_BY_CUSTOMERS)
                .and()
                .withExternal()
                .source(AUDIT_WATING_BY_CDM).target(AUDIT_PASSED_BY_CDM)
                .event(AUDIT_PASS_BY_CDM)
                .and()
                .withExternal()
                .source(AUDIT_WATING_BY_CDM).target(AUDIT_REFUSED_BY_CDM)
                .event(AUDIT_REFUSE_BY_CDM)
                .and()
                .withExternal()
                .source(AUDIT_PASSED_BY_CDM).target(DISPATCHED)
                .event(DISPATCH_ID)
                .and()
                .withExternal()
                .source(DISPATCHED).target(AUDIT_PASSED_BY_SOCIAL)
                .event(AUDIT_PASS_BY_SOCIAL)
                .and()
                .withExternal()
                .source(AUDIT_PASSED_BY_SOCIAL).target(SEND_OMS_SUCCESS)
                .event(SEND_OMS)
                .and()
                .withExternal()
                .source(SEND_OMS_SUCCESS).target(SEND_GOODS_SUCCESS)
                .event(SEND_GOODS)
                .and()
                .withExternal()
                //只要未发货状态都可以未发货取消
                .source(GENERATED).source(AUDIT_WATING_BY_CDM).source(AUDIT_PASSED_BY_CDM).source(AUDIT_REFUSED_BY_CDM).source(DISPATCHED)
                .target(CANCELED_BY_CUSTOMERS)
                .event(CANCEL_BY_CUSTOMERS);
    }
    @Override
    public void configure(StateMachineConfigurationConfigurer<OrderStatus, OrderEvent> config) throws Exception {
        config
            .withDistributed()
            .ensemble(stateMachineEnsemble());
        config.withConfiguration()
                .autoStartup(true)
                .listener(stateMachineDaoListener);

    }
    @Override
    public void configure(StateMachineStateConfigurer<OrderStatus, OrderEvent> states)
            throws Exception {
        states
            .withStates()
            .initial(GENERATED)
            .states(EnumSet.allOf(OrderStatus.class));
    }
    @Bean
    public CuratorFramework curatorClient() throws Exception {
        CuratorFramework client = CuratorFrameworkFactory.builder().defaultData(new byte[0])
                .retryPolicy(new ExponentialBackoffRetry(1000, 3))
                .connectString("localhost:2181").build();
        client.start();
        return client;
    }
}
